package com.nf.health.app.inquiry.adapter;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.nf.health.app.R;
import com.nf.health.app.customview.CircleImageView;
import com.nf.health.app.customview.MyListView;
import com.nf.health.app.inquiry.bean.DoctorListClickBean;
import com.nf.health.app.inquiry.bean.InquiryDetailBean;
import com.nf.health.app.utils.DateUtil;
import com.nf.health.app.utils.ImageLoaderUtils;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by hepann on 2016/9/26.
 * The only genius that is worth anything is the genius for hard work
 *
 * @author hepann
 * @Description
 * @email he_pann@163.com
 */
public class DetailDoctorListAdapter extends BaseAdapter implements View.OnClickListener {
    private InquiryDetailBean mDetailBean;
    private ArrayList<InquiryDetailBean.DoctorlistBean> doctorlist;
    private Context ctx;

    public DetailDoctorListAdapter(InquiryDetailBean mDetailBean, Context ctx) {
        this.mDetailBean = mDetailBean;
        this.doctorlist = mDetailBean.getDoctorlist();
        this.ctx = ctx;
    }

    public void setInquiryDetailBean(InquiryDetailBean mDetailBean) {
        this.mDetailBean = mDetailBean;
        this.doctorlist = mDetailBean.getDoctorlist();
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        return doctorlist.size();
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        final InquiryDetailBean.DoctorlistBean bean = doctorlist.get(position);
        final ViewHolder holder;
        if (convertView == null) {
            convertView = View.inflate(ctx, R.layout.item_detail_doctor_list, null);
            holder = new ViewHolder(convertView);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        holder.tvDoctorName.setText(bean.getNick());
        ImageLoaderUtils.getinstance(ctx).getImage(holder.ivDoctorHimg, bean.getHimg(), R.drawable.default_image);
        holder.tvDoctorHospital.setText(bean.getHospitalname());
        holder.tvDoctorProfessional.setText(bean.getDepartment() + "|" + bean.getProfessional());
        //设置关注背景图片
        if ("1".equals(bean.getAttention())) {
            //已经关注情况
            holder.ivAttention.setBackgroundResource(R.drawable.detail_inquiry_attention_light);
            holder.tvAttention.setText("已关注");
        } else {
            //没有关注的情况
            holder.ivAttention.setBackgroundResource(R.drawable.detail_inquiry_attention_dark);
            holder.tvAttention.setText("关注");
        }
        //设置三个服务背景图片,咨询,家庭服务,健康管理
        if ("2".equals(bean.getConsult())) {
            holder.ivConsult.setBackgroundResource(R.drawable.detail_inquiry_consult);
        } else {
            holder.ivConsult.setBackgroundResource(R.drawable.detail_inquiry_consult_gray);
        }
        if ("2".equals(bean.getFamily())) {
            holder.ivFamilyService.setBackgroundResource(R.drawable.detail_inquiry_family_service);
        } else {
            holder.ivFamilyService.setBackgroundResource(R.drawable.detail_inquiry_family_service_gray);
        }
        if ("2".equals(bean.getSound())) {
            holder.ivHealthManager.setBackgroundResource(R.drawable.detail_inquiry_health_manage);
        } else {
            holder.ivHealthManager.setBackgroundResource(R.drawable.detail_inquiry_health_manage_gray);
        }
        //设置聊天详情
        setMessageAdapter(holder.lvMessage, bean.getInquiryreplylist());

        //设置tag,以便相应点击事件
        holder.ivDoctorHimg.setTag(new DoctorListClickBean(DoctorListClickBean.DOCTOR_HIMG, position));
//        holder.ivAttention.setTag(new DoctorListClickBean(DoctorListClickBean.ATTENTION, position));
        holder.llAttention.setTag(new DoctorListClickBean(DoctorListClickBean.ATTENTION, position));
//        holder.ivGift.setTag(new DoctorListClickBean(DoctorListClickBean.GIFT, position));
        holder.llGift.setTag(new DoctorListClickBean(DoctorListClickBean.GIFT, position));
        //用户追问数量未到,并且小于48小时
        String hour = DateUtil.formatDuring(System.currentTimeMillis() - mDetailBean.getInquiry().getCreatetime());
        if (mDetailBean.getInquiry().getUserreplynum() < 10 && Integer.valueOf(hour) < 48) {
            holder.ivComment.setBackgroundResource(R.drawable.detail_inquiry_reply);
            holder.tvComment.setText("追问");
            holder.llComment.setTag(new DoctorListClickBean(DoctorListClickBean.REPLY_DOCTOR, position));
        } else {
            //已经追问完毕,分为已评未评两种情况
            if ("0".equals(bean.getNotescoreid())) {
                holder.ivComment.setBackgroundResource(R.drawable.detail_inquiry_comment_yes);
                holder.tvComment.setText("评论");
                holder.llComment.setTag(new DoctorListClickBean(DoctorListClickBean.COMMENT, position));
            } else {
                //应经评论的情况
                holder.ivComment.setBackgroundResource(R.drawable.detail_inquiry_comment_no);
                holder.tvComment.setText("已评论");
            }
        }
        holder.llConsult.setTag(new DoctorListClickBean(DoctorListClickBean.CONSULT, position));
        holder.llFamilyService.setTag(new DoctorListClickBean(DoctorListClickBean.FAMILY_SERVICE, position));
        holder.llHealthManager.setTag(new DoctorListClickBean(DoctorListClickBean.HEALTH_MANAGE, position));
        holder.rlBottom.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //展开消息
                if (holder.lvMessage.getVisibility() == View.GONE) {
                    holder.lvMessage.setVisibility(View.VISIBLE);
                    holder.ivBottomArrow.setBackgroundResource(R.drawable.inquiey_detail_arrowup);
                    holder.tvBottomHint.setText("收起详情");
                    holder.tvNewReply.setVisibility(View.GONE);
                    holder.ivNewReply.setVisibility(View.GONE);
//                    holder.lvMessage.scrollTo(0, ScreentUtils.getScreenHeight(ctx));
                    holder.lvMessage.setSelection(position);
                    //如果是未读消息,去把其给位已读
                    if ("0".equals(bean.getReadtype())) {
                        EventBus.getDefault().post(new DoctorListClickBean(DoctorListClickBean.READ_REPLY, position));
                    }
                } else if (holder.lvMessage.getVisibility() == View.VISIBLE) {
                    holder.lvMessage.setVisibility(View.GONE);
                    holder.ivBottomArrow.setBackgroundResource(R.drawable.inquiey_detail_arrowdown);
                    holder.tvBottomHint.setText("查看详情");
                    //消息未读,0代表用户未读
                    if ("0".equals(bean.getReadtype())) {
                        holder.tvNewReply.setVisibility(View.VISIBLE);
                        holder.ivNewReply.setVisibility(View.VISIBLE);
                    } else {
                        holder.tvNewReply.setVisibility(View.GONE);
                        holder.ivNewReply.setVisibility(View.GONE);
                    }
                }
            }
        });
        initListener(holder);
        //是否有未读消息
        if ("0".equals(bean.getReadtype()) && holder.lvMessage.getVisibility() != View.VISIBLE) {
            holder.tvNewReply.setVisibility(View.VISIBLE);
            holder.ivNewReply.setVisibility(View.VISIBLE);
        } else {
            holder.tvNewReply.setVisibility(View.GONE);
            holder.ivNewReply.setVisibility(View.GONE);
        }
        return convertView;
    }

    private void initListener(ViewHolder holder) {
        holder.ivDoctorHimg.setOnClickListener(this);
        holder.llAttention.setOnClickListener(this);
        holder.llGift.setOnClickListener(this);
        holder.llComment.setOnClickListener(this);

        //三个服务
        holder.llConsult.setOnClickListener(this);
        holder.llFamilyService.setOnClickListener(this);
        holder.llHealthManager.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        Object tag = v.getTag();
        //避免空指针和其他类型的tag
        if (tag instanceof DoctorListClickBean) {
            DoctorListClickBean bean = (DoctorListClickBean) tag;
            //发送点击事件到activity
            EventBus.getDefault().post(bean);
        }
    }

    /**
     * 设置消息适配器
     *
     * @param lvMessage
     */
    public void setMessageAdapter(ListView lvMessage, List<InquiryDetailBean.DoctorlistBean.InquiryreplylistBean> bean) {
        MessageAdapter messageAdapter = new MessageAdapter(ctx, bean);
        lvMessage.setAdapter(messageAdapter);
    }

    static class ViewHolder {
        @Bind(R.id.iv_doctor_himg)
        CircleImageView ivDoctorHimg;
        @Bind(R.id.tv_doctor_name)
        TextView tvDoctorName;
        @Bind(R.id.tv_doctor_hospital)
        TextView tvDoctorHospital;
        @Bind(R.id.tv_doctor_professional)
        TextView tvDoctorProfessional;
        @Bind(R.id.iv_attention)
        ImageView ivAttention;
        @Bind(R.id.ll_attention)
        LinearLayout llAttention;
        @Bind(R.id.tv_attention)
        TextView tvAttention;
        @Bind(R.id.iv_gift)
        ImageView ivGift;
        @Bind(R.id.ll_gift)
        LinearLayout llGift;
        @Bind(R.id.tv_gift)
        TextView tvGift;
        @Bind(R.id.iv_comment)
        ImageView ivComment;
        @Bind(R.id.ll_comment)
        LinearLayout llComment;
        @Bind(R.id.tv_comment)
        TextView tvComment;
        @Bind(R.id.iv_consult)
        ImageView ivConsult;
        @Bind(R.id.tv_consult)
        TextView tvConsult;
        @Bind(R.id.ll_consult)
        LinearLayout llConsult;
        @Bind(R.id.iv_family_service)
        ImageView ivFamilyService;
        @Bind(R.id.tv_family_service)
        TextView tvFamilyService;
        @Bind(R.id.ll_family_service)
        LinearLayout llFamilyService;
        @Bind(R.id.iv_health_manager)
        ImageView ivHealthManager;
        @Bind(R.id.tv_health_manager)
        TextView tvHealthManager;
        @Bind(R.id.ll_health_manager)
        LinearLayout llHealthManager;
        @Bind(R.id.lv_message)
        MyListView lvMessage;
        @Bind(R.id.tv_new_reply)
        TextView tvNewReply;
        @Bind(R.id.iv_new_reply)
        ImageView ivNewReply;
        @Bind(R.id.tv_bottom_hint)
        TextView tvBottomHint;
        @Bind(R.id.iv_bottom_arrow)
        ImageView ivBottomArrow;
        @Bind(R.id.rl_bottom)
        RelativeLayout rlBottom;

        ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }
}
